package com.ilop.sthome.mvp.present.scene;

import android.content.Context;
import android.os.Handler;

import com.aliyun.iot.aep.sdk.apiclient.callback.IoTResponse;
import com.example.common.mvp.BasePresenterImpl;
import com.ilop.sthome.common.command.SendAutoCommand;
import com.ilop.sthome.data.greenDao.DeviceInfoBean;
import com.ilop.sthome.data.greenDao.SceneBean;
import com.ilop.sthome.mvp.contract.scene.SceneChangeContract;
import com.ilop.sthome.common.command.SendSceneCommand;
import com.ilop.sthome.mvp.model.CommonModel;
import com.ilop.sthome.mvp.model.common.CommonModelImpl;
import com.example.common.mvp.onModelCallBack;
import com.ilop.sthome.ui.dialog.BaseDialog;
import com.ilop.sthome.ui.dialog.TipDialog;
import com.ilop.sthome.utils.CoderUtils;
import com.ilop.sthome.utils.greenDao.utils.AutomationDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.DeviceDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneDaoUtil;
import com.ilop.sthome.utils.greenDao.utils.SceneSwitchDaoUtil;
import com.siterwell.familywellplus.R;

import java.util.List;

/**
 * @author skygge
 * @date 2020-01-14.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class SceneChangePresenter extends BasePresenterImpl<SceneChangeContract.IView> implements SceneChangeContract.present{

    private Context mContext;
    private int mSceneId;
    private String mDeviceName;
    private Handler mHandler;
    private SendSceneCommand mSendScene;

    public SceneChangePresenter(Context context, String deviceName) {
        this.mContext = context;
        this.mDeviceName = deviceName;
        mHandler = new Handler();
        DeviceInfoBean mDeviceInfo = DeviceDaoUtil.getInstance().findGatewayByDeviceName(mDeviceName);
        mSendScene = new SendSceneCommand(mContext, mDeviceInfo);
    }

    @Override
    public void getGatewayList() {
        CommonModelImpl mModel = new CommonModel(mHandler);
        mModel.onGetGatewayList(new onModelCallBack() {
            @Override
            public void onResponse(IoTResponse response) {
            }

            @Override
            public void onFailure(Exception e) {
            }
        });
    }

    @Override
    public void getSceneList() {
        List<SceneBean> mList = SceneDaoUtil.getInstance().findAllScene(mDeviceName);
        if (mList.size()>0){
            mHandler.post(()->mView.showSceneList(mList));
        }else {
            SceneDaoUtil.getInstance().addDefaultScene(mContext, mDeviceName);
            getSceneList();
        }
    }

    @Override
    public void changeScene(SceneBean scene) {
        if(scene.getChoice()!=1 && mDeviceName.equals(scene.getDeviceName())){
            mSceneId = scene.getSid();
            mSendScene.choseScene(mSceneId);
        }
    }

    @Override
    public void saveCurrentState() {
        SceneDaoUtil.getInstance().updateChoice(mSceneId, mDeviceName);
    }

    @Override
    public void synchronizationScene(String deviceName) {
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceName);
        if (deviceInfoBean != null) {
            SendSceneCommand mSend = new SendSceneCommand(mContext, deviceInfoBean);
            String scene_crc = CoderUtils.getSceneCRC(deviceName);
            mSend.synScene(scene_crc);
        }
    }

    @Override
    public void synchronizationAuto(String deviceName) {
        DeviceInfoBean deviceInfoBean = DeviceDaoUtil.getInstance().findGatewayByDeviceName(deviceName);
        if (deviceInfoBean != null) {
            SendAutoCommand mSend = new SendAutoCommand(mContext, deviceInfoBean);
            String autoCode = CoderUtils.getAutomationCode(deviceName);
            List<String> mCRCList = AutomationDaoUtil.getInstance().findAllAutoCRC(deviceName);
            int size = mCRCList.size();
            if (size == 0) {
                String page1 = "00";
                mSend.synAutomation(autoCode, page1);
            } else if (size < 128) {
                String page1 = CoderUtils.getAutomationCRCPage1(mCRCList);
                mSend.synAutomation(autoCode, page1);
            } else if (mCRCList.size() > 128) {
                String page1 = CoderUtils.getAutomationCRCPage1(mCRCList);
                String page2 = CoderUtils.getAutomationCRCPage2(mCRCList);
                mSend.synAutomationWithPage2(autoCode, page1, page2);
            }
        }
    }

    @Override
    public void showDialogToDelete(SceneBean scene) {
        if (scene.getSid() < 3){
            String title = mContext.getString(R.string.system_mode_not_allow_delete);
            showDialog(title);
        }else {
            int mCurrentScene = SceneDaoUtil.getInstance().findSceneByChoice(scene.getDeviceName()).getSid();
            if (scene.getSid() == mCurrentScene){
                String title = mContext.getString(R.string.can_t_delete_the_current_scene);
                showDialog(title);
            }else {
                String sd = String.format(mContext.getResources().getString(R.string.want_to_delete_confirm_eq), scene.getModleName());
                TipDialog dialog = new TipDialog(mContext, ()->{
                    mView.showProgress();
                    mView.setDeleteSceneId(scene.getSid());
                    mSendScene.deleteScene(scene.getSid());
                });
                dialog.setMsg(sd);
                dialog.show();
            }
        }
    }

    @Override
    public void deleteSceneSuccess(int sceneId) {
        SceneDaoUtil.getInstance().deleteBySid(sceneId, mDeviceName);
        SceneSwitchDaoUtil.getInstance().deleteAllSwitch(sceneId, mDeviceName);
        mView.showToastMsg(mContext.getString(R.string.delete_success));
        mView.setDeleteSceneId(-1);
        getSceneList();
    }

    private void showDialog(String title){
        BaseDialog mDialog = new BaseDialog(mContext);
        mDialog.setCancelVisibility(true);
        mDialog.setMsg(title);
        mDialog.setCancelText(mContext.getString(R.string.initialzation_guidance_button));
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.show();
    }
}
